package com.pro.control;

import com.pro.dao.DeptMapper;
import com.pro.entity.DeptPlus;
import com.pro.entity.JsonData;
import com.pro.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @ClassName: DeptController
 * @Author: 笑笑
 * @DATE: 2021/11/9
 * @TIME: 上午 10:36
 */
@RestController
@RequestMapping("/Dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //查询所有部门信息
    @RequestMapping("/getAllDept")
    public JsonData getAllDept(DeptPlus deptPlus) {
        List<DeptPlus> allDept = deptService.getAllDept(deptPlus);
        return allDept != null ? JsonData.buildSuccess(allDept) : JsonData.buildError("服务器错误");
    }

    //根据id查询部门信息
    @RequestMapping("/getById")
    public JsonData getById(Integer id) {
        DeptPlus deptPlus = null;
        try {
            deptPlus = deptService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deptPlus != null ? JsonData.buildSuccess(deptPlus) : JsonData.buildError("服务器错误");
    }

    //根据id修改部门信息
    @RequestMapping("/updateDept")
    public JsonData updateDept(DeptPlus deptPlus) {
        Boolean aBoolean = null;
        try {
            aBoolean = deptService.updateDept(deptPlus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aBoolean != null ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("服务器错误");
    }

    //根据id删除部门信息
    @RequestMapping("/deleteDept")
    public JsonData deleteDept(Integer id) {
        Boolean aBoolean = null;
        try {
            aBoolean = deptService.deleteDept(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aBoolean != null ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("服务器错误");
    }

    //添加部门信息
    @RequestMapping("/addDept")
    public JsonData addDept(DeptPlus deptPlus) {
        Boolean aBoolean = deptService.addDept(deptPlus);
        return aBoolean != null ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("服务器错误");
    }

    //查询所有部门信息——树
    @RequestMapping("/getDeptZtree")
    public JsonData getDeptZtree() {
        Map<String, Object> map = deptService.getDeptZtree();
        return map != null ? JsonData.buildSuccess(map) : JsonData.buildError("服务器错误");
    }

    //根据部门名称查询id
    @RequestMapping("/getNameById")
    public JsonData getNameById(String deptName) {
        Integer id = deptService.getNameById(deptName);
        return id > 0 ? JsonData.buildSuccess(id) : JsonData.buildError("服务器错误");
    }
}
